<template>
  <div class="search-container" v-loading="loading">
    <el-form v-model="searchPage" inline="true" >
      <el-form-item  v-for="(item, index) in searchPage.items" :key="index" :label="$t('msg.search.'+item.name)">
        <el-select  v-if="item.disabled != false && item.itemType === 'DICT'" v-model="item.value" :multiple = "item.mutSelect ==='checkBox'">
          <el-option  v-for="(itemChild, itemIndex) in item.dict" :key="itemIndex"  :label="itemChild.value" :value="itemChild.key" />
        </el-select>
        <el-input v-else-if="item.itemType !== 'DATE'" v-model="item.value"></el-input>
        <el-col :span="24" v-else>
          <el-form-item>
            <el-date-picker
              v-model="item.value"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{$t('msg.common.search')}}</el-button>
        <el-button @click="onReset">{{$t('msg.common.reset')}}</el-button>
        <el-button @click="showApplyInfo">申请详情</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="data-container">
      <el-table :data="tableData" style="width: 100%" highlight-current-row @current-change="handleCurrentRow">
      <el-table-column type="index" width="80" label="序号"/>
      <el-table-column prop="applyStatus" label="状态" width="120">
        <template #default="scope">
          <el-select disabled v-model="scope.row.applyStatus">
            <el-option label="创建" value="CREATE"></el-option>
            <el-option label="申请" value="APPLY"></el-option>
            <el-option label="通过" value="AUDIT"></el-option>
            <el-option label="驳回" value="REJECT"></el-option>
            <el-option label="取消" value="CANCEL"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="job.code" label="岗位代码" width="120" />
      <el-table-column prop="job.name" label="岗位名称" width="200" />
      <el-table-column prop="job.publishDate" label="岗位发布日期" width="120" />
      <el-table-column prop="job.endDate" label="岗位截止日期" width="120" />
      <el-table-column fixed="right" :label="$t('msg.common.operation')" width="140">
        <!-- vue3中 solt-scop已经放弃使用 -->
        <template #default="scope">
          <el-button text type="danger" size="small" @click="cancel(scope.row)">取消申请</el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      v-model:currentPage="searchPage.current"
      v-model:page-size="searchPage.pageSize"
      :page-sizes="[20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.total"
      hide-on-single-page=true
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      v-model="dialogVisible"
      title="申请详情"
      width="30%"
      :before-close="handleClose"
    >
      <el-table
        :data="applyLogs"
        style="width: 100%"
        :row-class-name="tableRowClassName"
      >
        <el-table-column prop="createTime" label="操作日期" width="180" />
        <el-table-column prop="user.userName" label="用户账号" width="180" />
        <el-table-column prop="applyStatus" label="申请状态" width="180">
          <template #default="scope">
            <el-select disabled v-model="scope.row.applyStatus">
              <el-option label="创建" value="CREATE"></el-option>
              <el-option label="申请" value="APPLY"></el-option>
              <el-option label="通过" value="AUDIT"></el-option>
              <el-option label="驳回" value="REJECT"></el-option>
              <el-option label="取消" value="CANCEL"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="notes" label="备注" width="180" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { ownList, logicDeleteById, showApply } from '@/api/applicant/userApply'
import { ElMessage } from 'element-plus'
import { useI18n } from 'vue-i18n'

const tableDatas = ref({})

const currentRow = ref({})

const dialogVisible = ref(false)

const applyLogs = ref([])

const loading = ref(false)

const i18n = useI18n()

const searchPage = ref({
  items: [
    {
      name: 'applyStatus',
      itemType: 'DICT',
      dict: [
        {
          key: 'CREATE',
          value: '创建'
        },
        {
          key: 'APPLY',
          value: '申请'
        },
        {
          key: 'AUDIT',
          value: '通过'
        },
        {
          key: 'REJECT',
          value: '驳回'
        },
        {
          key: 'CANCEL',
          value: '取消'
        }
      ],
      sign: '==',
      value: ''
    }
  ],
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const tableData = ref([])

const searchFunction = async () => {
  ownList(searchPage.value).then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success(i18n.t('msg.common.success'))
  }).catch(err => {
    ElMessage.success(i18n.t('msg.common.error') || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    items: [
      {
        name: 'applyStatus',
        itemType: 'STRING',
        dict: [
          {
            key: 'CREATE',
            value: '创建'
          },
          {
            key: 'APPLY',
            value: '申请'
          },
          {
            key: 'AUDIT',
            value: '通过'
          },
          {
            key: 'REJECT',
            value: '驳回'
          },
          {
            key: 'CANCEL',
            value: '取消'
          }
        ],
        sign: '==',
        value: ''
      }
    ],
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

const cancel = id => {
  logicDeleteById(id).then(res => {
    console.log(res)
  })
}

const showApplyInfo = () => {
  applyLogs.value = []
  if (Object.keys(currentRow.value).length === 0) {
    ElMessage.info('请选择需要查看的申请数据')
    return
  }
  dialogVisible.value = true
  showApply(currentRow.value.id).then(res => {
    console.log(res)
    applyLogs.value = res
  })
}

const handleCurrentRow = val => {
  currentRow.value = val
}

const handleClose = () => {
  dialogVisible.value = false
  applyLogs.value = []
}

const tableRowClassName = (row, rowIndex) => {
  if (rowIndex % 2 === 0) {
    return 'warning-row'
  } else {
    return 'success-row'
  }
}
</script>

<style lang="scss" scope>
.search-container {
  width: 100%;
  height: 100px;
}
.el-table .warning-row {
  background-color: cyan;
}

.el-table .success-row {
  background-color:burlywood;
}
</style>
